package com.jf3q.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jf3q.admin.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduTeacher;
import com.jf3q.common.utils.OssUtils;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.common.vo.SearchTeacherVo;
import com.jf3q.common.vo.TeacherVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
 * @author:xiaojie
 * @create: 2023-04-25 14:08
 * @Description: 教师controller
 */

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    EduTeacherService eduTeacherService;
    @Autowired
    OssUtils ossUtils;

    @Value("${redis.newTeacherKey}")
    String newTeacherKey;

    @Autowired
    StringRedisTemplate redisTemplate;

    @PostMapping("/uploadAvatar")
    public ResultVo uploadAvatar(MultipartFile file){
        return ResultVo.success("上传成功",ossUtils.upload("teacher",file));

    }

    @PostMapping("/save")
    @Operation(summary = "讲师保存接口")
    @PreAuthorize("hasAuthority('teacher:save')")
    public ResultVo save(@RequestBody TeacherVo teacherVo){

        EduTeacher eduTeacher = new EduTeacher();
        BeanUtils.copyProperties(teacherVo,eduTeacher);
        eduTeacherService.saveOrUpdate(eduTeacher);

        if(StringUtils.checkValNotNull(teacherVo.getId())){
            //修改
            Set<String> range = redisTemplate.opsForZSet().range(newTeacherKey, 0, -1);

        }

        return ResultVo.success("添加讲师成功",null);

    }
    @PostMapping("page/{pageNum}")
    @Operation(summary = "讲师分页查询接口")
    @PreAuthorize("hasAuthority('teacher:list')")
    public ResultVo get(@PathVariable Integer pageNum,
                        @RequestBody(required = false) SearchTeacherVo searchTeacherVo){

        Page<EduTeacher> page=eduTeacherService.getPage(pageNum,searchTeacherVo);
        return ResultVo.success("查询所有讲师",page);
    }



    @Operation(summary = "删除讲师")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('teacher:del')")
    public ResultVo del(@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);

        if (flag) {
            return ResultVo.success("删除成功",null);
        }
        return ResultVo.error("删除失败");

    }
    @GetMapping("/{id}")
    public ResultVo getById(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return ResultVo.success("", teacher);
    }


    private Long indexOfNewTeachers(String teacherId) {
        // 获取课程列表
        List<String> range = redisTemplate.opsForList().range(newTeacherKey, 0, -1);

        // 找到文章的索引
//        for (int i = 0; i < courses.size(); i++) {
//            if (courses.get(i).getId().equals(courseId)) {
//                return Long.valueOf(i);
//            }
//        }
        return -1L; // 课程不在列表中
    }

}
